function goToFriends() {
    window.location.href = "friends.html"
}
function goToPyq() {
    window.location.href = 'pyq.html';
}
function goToMessage() {
    window.location.href = 'main.html'
}
function goToGroup() {
    window.location.href = 'group.html'
}
document.addEventListener('click', function(event) {
    var contextMenu = document.getElementById('contextMenu');
    contextMenu.style.display = 'none';
});

function showMessageBox() {
    var messageBox = document.getElementById("messageBox");
    messageBox.style.display = "block";
    setTimeout(function(){
        messageBox.style.display = "none";
    }, 2000);
}
function unsetInput(element) {
    element.querySelector("input").classList.remove('reset');
    element.querySelector("input").placeholder = '搜索';
    //清除
    var searchDiv = document.querySelector(".search-div");
    var sibling = searchDiv.nextSibling;
    // 移除searchDiv之后的所有兄弟元素
    while (sibling) {
        var nextSibling = sibling.nextSibling;
        sibling.parentNode.removeChild(sibling);
        sibling = nextSibling;
    }

    init();
    document.querySelector("#x").style.display = 'none';

}
function resetInput(element) {


    element.classList.add('reset');
    document.querySelector("#x").style.display = 'block';
    element.placeholder = '';

    // clearList();
    var searchDiv = document.querySelector(".search-div");
    var sibling = searchDiv.nextSibling;
    // 移除searchDiv之后的所有兄弟元素
    while (sibling) {

        var nextSibling = sibling.nextSibling;
        sibling.parentNode.removeChild(sibling);
        sibling = nextSibling;
    }

}
function searchMessage(element) {
    var searchDiv = document.querySelector(".search-div");
    var sibling = searchDiv.nextSibling;

    // 移除searchDiv之后的所有兄弟元素
    while (sibling) {
        if (sibling.classList && (sibling.classList.contains("search-friend") || sibling.classList.contains("search-box"))) {
            var nextSibling = sibling.nextSibling;
            sibling = nextSibling;
            continue;
        }
        var nextSibling = sibling.nextSibling;
        sibling.parentNode.removeChild(sibling);
        sibling = nextSibling;
    }
    var key = element.value;
    fetch(`http://localhost/message/searchMessage?key=${key}`, {
        method: 'GET', // 指定请求方法为 GET
        headers: {
            'Content-Type': 'application/json',// 指定请求头
            'Authorization': localStorage.getItem("token")
        }
    })
        .then(response => {
            if (!response.ok) {
                throw new Error('网络响应不正常');
            }
            return response.json();
        })
        .then(data => {
            if (data.flag) {
                const userListDiv = document.getElementById('user-list');
                data.data.forEach(item => {
                    var user;
                    if (item.note != null) {
                        user = item.note;
                    }else{
                        user = item.friend_name;
                    }
                    var userItem = document.createElement('div');
                    userItem.classList.add('user-item');
                    userItem.setAttribute('onclick', `showUserInfo('${user}','${item.friend_name}',this)`);

                    var userAvatar = document.createElement('img');
                    userAvatar.classList.add('user-avatar');
                    userAvatar.setAttribute('src', './favicon.ico');
                    userItem.appendChild(userAvatar);

                    var userName = document.createElement('div');
                    userName.classList.add('user-name');
                    userName.textContent = user;
                    userItem.appendChild(userName);

                    // 将新的用户条目元素添加到 userListDiv 中
                    userListDiv.appendChild(userItem);

                });

            }else {
                console.error('根据关键字搜索消息失败:', data.message);
            }
        })
        .catch(error => {
            console.error('根据关键字搜索消息出现问题:', error);
        });
}

function openModal() {
    document.getElementById('myModal').style.display = "flex";
}
function closeModal() {
    // 关闭模态框
    document.getElementById('myModal').style.display = "none";
    document.getElementById('groupName').value = "";
}

function cancel() {
    // 取消操作
    closeModal();
}

function confirm() {
    // 确认操作
    var groupName = document.getElementById('groupName').value.trim();
    if (groupName.trim() === '') {
        alert('请输入群聊的名称。');
        return;
    }
    // 创建群聊
    createGroup();
    // 关闭模态框
    closeModal();
}

// 点击模态框外部区域关闭模态框
window.onclick = function(event) {
    var modal = document.getElementById('myModal');
    if (event.target == modal) {
        closeModal();
    }
}
function createGroup() {

    var groupName = document.getElementById("groupName").value;
    fetch(`http://localhost/group/createGroup`, {
        method: 'POST', // 指定请求方法为 GET
        headers: {
            'Content-Type': 'application/json',// 指定请求头
            'Authorization': localStorage.getItem("token")
        },
        body: JSON.stringify({ groupName: groupName })
    })
        .then(response => {
            if (!response.ok) {
                throw new Error('网络响应不正常');
            }
            return response.json();
        })
        .then(data => {
            if (data.flag) {
                document.getElementById("messageBox").innerText = "创建群聊成功";
                showMessageBox();
            }else {
                console.error('创建群聊失败:', data.message);
            }
        })
        .catch(error => {
            console.error('创建群聊出现问题:', error);
        });
}
